package com.jme.s3d.modelImporter.md5Importer;



import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.s3d.Main;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.resource.MultiFormatResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import com.model.md5.importer.MD5Importer;


/**
 * <code>MD5Viewwer</code> allows artists to use <code>JFileChooser</code> to choose
 * the selected MD5 files for viewing easily.
 * 
 * @author Yi Wang (Neakor)
 * @version Creation date: 05-21-08 11:30
 * @version Modified date: 08-01-08 11:22
 */
public class md5Importer{
	
	URL meshUrl;
	URL animUrl;
	
	Spatial model;
	
	public Spatial getModel() {
		return model;
	}
	public md5Importer(Node rootNode) {
		try {
			meshUrl = Main.class.getClassLoader().getResource("data/model/md5/dragon.md5mesh");
			animUrl = Main.class.getClassLoader().getResource("data/model/md5/dragon.md5anim");
			this.overrideTextureKey();
			// Load only mesh.
			MD5Importer.getInstance().loadMesh(meshUrl, "Mesh");
			// Load mesh and animation.
			MD5Importer.getInstance().load(meshUrl, "Mesh",animUrl, "Animation", Controller.RT_WRAP);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		model = (Spatial) MD5Importer.getInstance().getMD5Node();
		Quaternion q = new Quaternion(0.0f,0.5f,0.5f,0.0f);
//		model.setLocalRotation(new Matrix3f(
//				1.0f,0.5f,1.0f,
//				0.0f,1.0f,0.0f,
//				1.0f,1.5f,1.0f));
		model.setLocalRotation(q);
		model.setLocalScale(10);
		model.setLocalTranslation(new Vector3f(0,112,-200));
		rootNode.attachChild(model);
		MD5Importer.getInstance().cleanup();
	}
	/**
	 * Override the <code>Texture</code> key location.
	 */
	private void overrideTextureKey() {
		try {
			URL url = Main.class.getClassLoader().getResource("data/texture/md5Texture/");
			MultiFormatResourceLocator locator = new MultiFormatResourceLocator(url,new String[]{".tga"});
			System.out.println(locator.toString());
			ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, locator);
		} catch (URISyntaxException e) {e.printStackTrace();}
	}
}
